<?php
require_once('model.php');
require_once('brewery_model.php');

/**
 * beer_model Class
 * Model for the beer table
 * 
 * @author Victor Nava
 *
 */
class beer_model extends model {

	function __construct(){
		parent::__construct('beers');
	}

	/**
	 * Same as model but gets some associated content
	 * @see app/model#get($id)
	 */
	function get($id){
		$beer = parent::get($id);
		return $this->addRelatedContent($beer);
	}
	
	/**
	 * Adds the the related content (brewery, rating, type) to the array representing a beer 
	 * @param $beer
	 * @return Array representing one beer
	 */
	private function addRelatedContent(&$beer){
		$brewery = new brewery_model();
		$beerTypeModel = new model('beer_types');
		$beer['brewery'] = $brewery->get($beer['brewery_id']);
		$beer['rating'] = $this->getRating($beer['id']);
		$beer['type'] = $beerTypeModel->get($beer['beer_types_id']);
		unset($beer['beer_types_id']);
		unset($beer['brewery_id']);
		return $beer;
	}
	
	/**
	 * (non-PHPdoc)
	 * @see app/model#getAll($args)
	 */
	function getAll($args){
		$beerList = array();
		foreach(parent::getAll($args) as $beer){
			$beerList[] = $this->addRelatedContent($beer);
		}
		return $beerList;
	}	
	
	
	/**
	 * (non-PHPdoc)
	 * @see app/model#getAll($args)
	 */
	function getAll2($args){
		return parent::getAll();
	}
	
	/**
	 * Gets the brewery related to this beer
	 * @param $id
	 * @return Associative Array representing a brewery
	 */
	function getBrewery($id){
		$beer = $this->get($id);
		$brewery = new brewery_model();
		return $brewery->get($beer['brewery_id']);
	}

	/**
	 * (non-PHPdoc)
	 * @see app/model#create($args)
	 */
	function create($args){
		if(isset($args['name']) && isset($args['brewery_id'])){
			return parent::create($args);
		}
		return false;
	}

	/**
	 * Gets the rating (average) of a beer
	 * 
	 * @param $beerId
	 * @return int rating
	 */
	function getRating($beerId){
		$sql = "SELECT avg( rate_level_id ) FROM `beer_rates` WHERE beer_id=$beerId";

		try{
			$rate = $this->dbCon->query($sql)->fetch(PDO::FETCH_NUM);
			return round($rate[0]);
		}catch (PDOException $e){
			//TODO find proper way to display message?
			echo $e->getMessage();
		}
	}

	/**
	 * Get the number of rates of a beer
	 * 
	 * @param $beerId
	 * @return int rate count
	 */
	function getRateCount($beerId){
		$sql = "SELECT count(*) FROM `beer_rates` WHERE beer_id=$beerId";

		try{
			$count = $this->dbCon->query($sql)->fetch(PDO::FETCH_NUM);
			return round($count[0]);
		}catch (PDOException $e){
			//TODO find proper way to display message?
			echo $e->getMessage();
		}
	}

	/**
	 * Gets the list of countries that have beers
	 * 
	 * @return Array Countries
	 */
	function getCountryList(){
		$countryModel = new Model('countries');
		return $countryModel->getAll();
	}

	/**
	 * Gets all the beer types
	 * 
	 * @return Array beer types
	 */
	function getBeerTypeList(){
		$beerTypes = new Model('beer_types');
		return $beerTypes->getAll();
	}

	/**
	 * Search function
	 * 
	 * Search for a beer given by name and country
	 * 
	 * @param $args name, country, page number
	 * @return unknown_type
	 */
	function find($args=null){
		if(!isset($args)){
			return $this->getAll();
		}

		$beerName = isset($args['beer_name']) ? $args['beer_name'] : '';
		$countryName = isset($args['country_name']) ? $args['country_name'] : '';

		$sql = "SELECT beers.id FROM
				((beers LEFT JOIN breweries ON (beers.brewery_id = breweries.id))
				 LEFT JOIN beer_types ON (beer_types.id = beers.beer_types_id))
				  LEFT JOIN countries ON (breweries.countries_id = countries.id)
				   WHERE beers.name LIKE '%$beerName%' and countries.name LIKE '%$countryName%' ORDER BY beers.name";

		if(isset($args['page'])){
			$page = (int)$args['page'];
			$from = $page * $this->pageSize;
			$sql .= " LIMIT $from, $this->pageSize";
		}

		try{
			$beerTuples = $this->dbCon->query($sql)->fetchAll(PDO::FETCH_ASSOC);
		}catch (PDOException $e){
			//TODO find proper way to display message?
			echo $e->getMessage();
		}

		$beerList = array();

		if(!isset($beerTuples)){
			return array();
		}

		foreach($beerTuples as $beer){
			$beerList[] = $this->get($beer['id']);
		}
		return $beerList;
	}
}
?>